package edu.nuc.rubikrecord;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import edu.nuc.SQLoperation.SQLConstant;
import edu.nuc.SQLoperation.SQLiteHelper;
import edu.nuc.rubikstopwatch.R;
import edu.nuc.rubikstopwatch.Rubik;

public class RubikRecord extends ListActivity {
	final static private boolean DEBUG = true;
	final static private String TAG = "Yans-DEBUG";

	private ListView listView = null;
	// private SimpleAdapter adapter;
	private SimpleCursorAdapter cursorAdapter = null;
	private Cursor cursor = null;
	private SQLiteHelper DBHelper = null;

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		if (DEBUG) {
			Log.v(TAG, "onCreateContextMenu");
		}
        menu.add(1, 0, 0, R.string.menu_delete);  //添加两个菜单项
        menu.add(1, 1, 0, R.string.menu_copy);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case 0:{
			if (DEBUG) {
				Log.d(TAG, "deleting item");
			}
//			DBHelper = new SQLiteHelper(RubikRecord.this, SQLConstant.TABLENAME,null, SQLConstant.DATABASE_VERSION);
			AdapterContextMenuInfo contextMenu = (AdapterContextMenuInfo) item.getMenuInfo();
			if (DEBUG) {
				Log.v(TAG, String.valueOf(contextMenu.id));
			}
			DBHelper.delete(contextMenu.id);
			cursor.requery();
			Toast.makeText(this, R.string.delete_message, Toast.LENGTH_SHORT).show();
			break;
		}
		case 1:
			AdapterContextMenuInfo context = (AdapterContextMenuInfo) item.getMenuInfo();
			Cursor c = (Cursor)listView.getItemAtPosition(context.position);
			Rubik r = Rubik.parseCursor(c);
			ClipboardManager clip = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
			clip.setText(r.record);
		default:
			break;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.record);
		// Open & Manager Database
		DBHelper = new SQLiteHelper(RubikRecord.this, SQLConstant.TABLENAME,null, SQLConstant.DATABASE_VERSION);
		cursor = DBHelper.query("_id desc");
		startManagingCursor(cursor);

		listView = getListView();
		cursorAdapter = new SimpleCursorAdapter(RubikRecord.this,
				R.layout.recordlist, cursor,
				new String[] { "_score", "_data" }, new int[] {
						R.id.recordListRecordId, R.id.recordListTimeID });
		listView.setAdapter(cursorAdapter);
		listView.setCacheColorHint(0);
		registerForContextMenu(getListView());
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (DEBUG) {
			Log.d(TAG, "record_onDestroy");
		}
		if (cursor != null) {
			cursor.close();
		}
		if (DBHelper != null) {
			DBHelper.closedb();
			DBHelper.close();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Group ID
		int groupId = 0;
		// The order position of the item
		int menuItemOrder = Menu.NONE;
		// TODO Auto-generated method stub
		menu.add(groupId, 0, menuItemOrder, R.string.record_menu_clear);
		menu.add(groupId, 1, menuItemOrder, R.string.record_menu_fastest);
		menu.add(groupId, 2, menuItemOrder, R.string.record_menu_back);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		if (item.getGroupId()==0) {
			if (DEBUG) {
				Log.v(TAG, "func_onMenuItemSelected");
			}
			switch (item.getItemId()) {
			case 0:
				if (DBHelper != null) {
					DBHelper.deleteDatabase(this);
				}
				cursor.requery();
				break;
			case 1:
				Intent intent = new Intent(RubikRecord.this, StatsRecord.class);
				startActivity(intent);
				break;
			case 2:
				finish();
				break;
			default:
				break;
			}
			
		}
		return super.onMenuItemSelected(featureId, item);
	}

}
